VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   3090
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3090
   ScaleWidth      =   4680
   StartUpPosition =   3  'Windows Default
   Begin VB.Timer Tmr_Comm 
      Interval        =   100
      Left            =   3600
      Top             =   600
   End
   Begin MSWinsockLib.Winsock Sock 
      Index           =   0
      Left            =   1680
      Top             =   1440
      _ExtentX        =   741
      _ExtentY        =   741
      _Version        =   393216
      LocalPort       =   23
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim buff() As String


Private Sub Tmr_Comm_Timer()
Dim x As Integer
'we do everything here, each tick.

'Command "Tick"
ub = UBound(Players)
Dim play As Player

For x = 1 To ub
'Loop through all the players, and do their commands
Set play = Players(x)
If Not play.ready Then GoTo continue
Comm = Players(x).comstack.pop()
If Comm = "" Then GoTo continue
Debug.Print "Command recieved " & Comm
'Players(x).tme = 1000

Dim arg As String
sep = InStr(1, Comm, " ")
If sep = 0 Then sep = Len(Comm) + 1
com = Trim(Mid(Comm, 1, sep))
arg = Trim(Mid(Comm, sep))

cmd.setcid x 'Set the current player id for the next command
If (UCase(com) <> "LGN") And (Not Players(x).isloggedin) Then
    Sendto x, "MSG " & com & "? Login first!"
    GoTo continue:
End If


If InStr(1, COM_STR, "|" & UCase(com) & "|") > 0 Then 'Check if its one of our allowed commands
    CallByName cmd, com, VbMethod, arg
Else
    Sendto x, "MSG " & com & "? huh?" & vbCrLf 'If not, give a standard WTF? reply
End If

continue:
Next x
End Sub

Private Sub Form_Load()
Findroot 'Find the file root
Propinit 'Initialize my property bag
setini fpath & "Data\Config.ini"
Startdebug fpath & "Data\Log.htm" 'Start debugging

Loadrooms

Sock(0).Close 'Listen
Sock(0).Listen
ReDim buff(0)
ReDim Players(0)
Hide
End Sub

Private Sub Sock_Close(Index As Integer)
'close off the player stuff, etc. here
End Sub

Private Sub Sock_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Dim rip As String
Dim rhost As String
Dim banned As Boolean

For x = 1 To Sock.ubound
If Sock(x).state <> 7 Then
    GoTo sockfound:
End If
Next x

x = Sock.ubound + 1
ReDim Preserve buff(x)
ub = UBound(Players) + 1
ReDim Preserve Players(ub)

Load Sock(x)
sockfound:

Sock(x).Close
Sock(x).Accept requestID

If x > Max_Players Then
    Sock(x).SendData "MSG Sorry, the server is above capacity at this time, pleae come back later" & vbCrLf
    Sock(x).Close
End If

rip = Sock(x).RemoteHostIP 'Grab their ip
rhost = NameByAddr(rip)
banned = Checkban(rhost & "!" & rip)

For y = 1 To Sock.ubound 'Check to see if its a duplicate
If x <> y Then
    If Sock(y).RemoteHostIP = rip Then
        banned = True 'Comment out this line to allow duplicates
        Exit For
    End If
End If
Next y

If banned Then
    Sock(x).SendData "You have been banned from this server. If you think this was incorrect, please email the administrator at simplechat@gmail.com" & vbCrLf
    'Sleep 1000 'Give the socket time to send data
    Sock(x).Close 'and close their socket
    Exit Sub
End If

Debug.Print rip, rhost


'TODO add ban support
Debuglog "Accepted connection from " & Sock(x).RemoteHostIP
Sock(x).SendData Welcomemsg
End Sub

Private Sub Sock_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim x As String
Sock(Index).Getdata x, vbString, bytesTotal
If x = Chr(8) Then
    If Len(buff(Index)) > 0 Then
        buff(Index) = Left(buff(Index), Len(buff(Index)) - 1)
        Sock(Index).SendData " " & Chr(8)
    Else
        Sock(Index).SendData Chr(8) & " " & Chr(8)
    End If
ElseIf Left(x, 1) = Chr(9) Or Left(x, 1) = Chr(13) Then
    Do
    cbuff = buff(Index) & x         'grab all the data until the first newline, and add that to the command
    term = InStr(1, cbuff, vbCrLf)  'keep all the rest in the buffer
    cout = Mid(cbuff, 1, term - 1)
    buff(Index) = Mid(cbuff, term + 2)
    
    Debuglog "Recieved data " & Index & " : " & cout 'log data recieved
    'Sock(Index).SendData "> "
    Players(Index).comstack.push cout 'Add the command to the command stack
    Loop Until InStr(1, buff(Index), vbCrLf) = 0 'Keep going until there are no more commands
Else
    buff(Index) = buff(Index) & x
    If Len(buff(Index)) > 512 Then Sock(Index).Close 'If the command is too large, kick the user
End If
End Sub


